package com.sali.repository.commons;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;


public interface IBaseService<T> extends IService<T> {
    String ID = "id";
    // 状态修改
    boolean updateStatus(Long id,String column,Integer value);
    boolean updateStatusStr(Long id,String column,String value);

    // 自增
    boolean increById(Long id, String column);
    boolean increById(Long id, String column, Integer num);
    boolean increById(List<Long> ids, String column);
    boolean increById(List<Long> ids, String column, Integer num);

    // 减去
    boolean decreById(Long id, String column);
    boolean decreById(Long id, String column, Integer num);
    boolean decreById(List<Long> ids, String column);
    boolean decreById(List<Long> ids, String column, Integer num);

    /**
     * 自增
     * @param updateColumn 更新行
     * @param whereColumn where条件列
     * @param whereVal where条件值
     * 如： update T set updateColumn = updateColumn + 1 where whereColumn = whereVal
     */
    boolean incre(String updateColumn, String whereColumn,Long whereVal);

    /**
     * 递减
     * @param updateColumn 更新行
     * @param whereColumn where条件列
     * @param whereVal where条件值
     * 如： update T set updateColumn = updateColumn - 1 where whereColumn = whereVal
     */
    boolean decre(String updateColumn, String whereColumn,Long whereVal);

    /**
     * 递增
     * @param updateColumn 更新行
     * @param whereColumn where条件列
     * @param whereVal where条件值
     * @param num 递减值
     * 如： update T set updateColumn = updateColumn - num where updateColumn >= num and whereColumn = whereVal
     */
    boolean decre(String updateColumn, String whereColumn,Long whereVal, Integer num);

    /**
     * 批量递减
     * @param updateColumn 更新行
     * @param whereColumn where条件列
     * @param whereVals where条件值
     * 如： update T set updateColumn = updateColumn - 1 where whereColumn = whereVal
     */
    boolean decre(String updateColumn, String whereColumn,List<Long> whereVals);


    /**
     * @param clz
     * @param <T>
     * @param <R>
     * @return
     */
    default <T, R> R tranferBo(T pojo, Class<R> clz) {
        try {
            R r = clz.newInstance();
            BeanUtils.copyProperties(pojo, r);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * @param clz
     * @param <T>
     * @param <R>
     * @return
     */
    default <T, R> List<R> tranferListBo(List<T> list, Class<R> clz) {
        try {
            List<R> collect = list.stream()
                    .map(pojo -> this.tranferBo(pojo, clz))
                    .collect(Collectors.toList());
            return collect;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param clz
     * @param <T>
     * @param <R>
     * @return
     */
    default <T, R> Page<R> tranferPageBo(Page<T> poPage, Class<R> clz) {
        List<R> hotelBos = tranferListBo(poPage.getRecords(), clz);
        Page<R> boPage = new Page<>();
        boPage.setTotal(poPage.getTotal());
        boPage.setPages(poPage.getPages());
        boPage.setSize(poPage.getSize());
        boPage.setCurrent(poPage.getCurrent());
        boPage.setRecords(hotelBos);
        return boPage;
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    default Boolean copyData(Long opid){return false;}
}
